// import { Promise } from '../util';
var _DuoguanData = require('./data.js');
/**
 *  查询接口
 */
const API = _DuoguanData.duoguan_host_api_url + '/index.php?act=area&op=area_list&area_id=';
console.log(API);
const selectArea = {
  /**
   * 字符长度截取
   */
  addDot: function (arr) {
    if (arr instanceof Array) {
      arr.map(val => {
        if (val.area_name.length > 4) {
          val.area_nameDot = val.area_name.slice(0, 4) + '...';
          return val;
        } else {
          val.area_nameDot = val.area_name;
          return val;
        }
      })
    }
  },
  /**
   * 初始化区域数据
   */
  load: function (_this, opt) {
    _this.setData({
      isShow: false, // 显示区域选择框
      showDistrict: true // 默认为省市区三级区域选择
    });
    if (opt && !opt.showDistrict) {
      _this.setData({
        showDistrict: false
      });
    }
    Promise(wx.request, {
      url: API + '0',
      method: 'GET'
    }).then((province) => {

      const firstProvince = province.data.datas.area_list[0];

      selectArea.addDot(province.data.datas.area_list);
      /**
       * 默认选择获取的省份第一个省份数据
       */
      _this.setData({
        proviceData: province.data.datas.area_list,
        'selectedProvince.index': 0,
        'selectedProvince.area_id': firstProvince.area_id,
        'selectedProvince.area_name': firstProvince.area_name,
      });
      return (
        Promise(wx.request, {
          url: API + firstProvince.area_id,
          method: 'GET'
        })
      );
    }).then((city) => {
      const firstCity = city.data.datas.area_list[0];
      selectArea.addDot(city.data.datas.area_list);
      _this.setData({
        cityData: city.data.datas.area_list,
        'selectedCity.index': 0,
        'selectedCity.area_id': firstCity.area_id,
        'selectedCity.area_name': firstCity.area_name
      });
      /**
       * 省市二级则不请求区域
       */
      if (_this.data.showDistrict) {
        return (
          Promise(wx.request, {
            url: API + firstCity.area_id,
            method: 'GET'
          })
        );
      } else {
        return;
      }
    }).then((district) => {
      const firstDistrict = district.data.datas.area_list[0];

      selectArea.addDot(district.data.datas.area_list);
      _this.setData({
        districtData: district.data.datas.area_list,
        'selectedDistrict.index': 0,
        'selectedDistrict.area_id': firstDistrict.area_id,
        'selectedDistrict.area_name': firstDistrict.area_name
      });
    }).catch((e) => {

    })
  },
  /**
   * 点击省份
   */
  tapProvince: function (e, _this) {
    const dataset = e.currentTarget.dataset;
    Promise(wx.request, {
      url: API + dataset.area_id,
      method: 'GET'
    }).then((city) => {
      if (city.data.datas.area_list) {
        selectArea.addDot(city.data.datas.area_list);


        _this.setData({
          cityData: city.data.datas.area_list,
          'selectedProvince.area_id': dataset.area_id,
          'selectedProvince.area_name': dataset.areaName,
          'selectedCity.area_id': city.data.datas.area_list[0].area_id,
          'selectedCity.area_name': city.data.datas.area_list[0].area_name
        });

        if (_this.data.showDistrict) {
          return (
            Promise(wx.request, {
              url: API + city.data.datas.area_list[0].area_id,
              method: 'GET'
            })
          );
        } else {
          _this.setData({
            'selectedProvince.index': dataset.index,
            'selectedCity.index': 0 // 默认显示第一个city
          })
          return;
        }


      } else {
        _this.setData({
          cityData: [],
          'selectedProvince.area_id': dataset.area_id,
          'selectedProvince.area_name': dataset.area_name
        })
      }

    }).then((district) => {
      if (district.data.datas.area_list) {
        selectArea.addDot(district.data.datas.area_list);
        _this.setData({
          districtData: district.data.datas.area_list,
          'selectedProvince.index': dataset.index,
          'selectedCity.index': 0,
          'selectedDistrict.index': 0,
          'selectedDistrict.area_id': district.data.datas.area_list[0].area_id,
          'selectedDistrict.area_name': district.data.datas.area_list[0].area_name
        });
      } else {
        _this.setData({
          districtData: [],
          'selectedProvince.index': dataset.index
        });
      }

    }).catch((error) => {

    })
  },
  /**
   * 点击城市
   */
  tapCity: function (e, _this) {
    const dataset = e.currentTarget.dataset;
    if (_this.data.showDistrict) {
      Promise(wx.request, {
        url: API + dataset.area_id,
        method: 'GET'
      }).then((district) => {
        if (district.data.datas.area_list) {
          selectArea.addDot(district.data.datas.area_list);
          _this.setData({
            districtData: district.data.datas.area_list,
            'selectedCity.index': dataset.index,
            'selectedCity.area_id': dataset.area_id,
            'selectedCity.area_name': dataset.areaName,
            'selectedDistrict.index': 0,
            'selectedDistrict.area_id': district.data.datas.area_list[0].area_id,
            'selectedDistrict.area_name': district.data.datas.area_list[0].area_name
          });
        } else {
          _this.setData({
            districtData: [],
            'selectedCity.index': dataset.index,
            'selectedCity.area_id': dataset.area_id,
            'selectedCity.area_name': dataset.area_name,
            'selectedDistrict.index': 0,
            'selectedDistrict.area_id': "",
            'selectedDistrict.area_name': ""
          });
        }

      }).catch((error) => {

      })
    } else {
      _this.setData({
        'selectedCity.index': dataset.index,
        'selectedCity.area_id': dataset.area_id,
        'selectedCity.area_name': dataset.area_name
      });
    }

  },
  /**
   * 点击区域
   */
  tapDistrict: function (e, _this) {
    const dataset = e.currentTarget.dataset;
    console.log(dataset)
    _this.setData({
      'selectedDistrict.index': e.currentTarget.dataset.index,
      'selectedDistrict.area_id': dataset.area_id,
      'selectedDistrict.area_name': dataset.areaName
    });
  },
  /**
   * 确认选择区域
   */
  confirm: function (e, _this) {
    console.log(_this.data);
    _this.setData({
      area_info: _this.data.showDistrict ? _this.data.selectedProvince.area_name + ' ' + _this.data.selectedCity.area_name + ' ' + _this.data.selectedDistrict.area_name : _this.data.selectedProvince.area_name + ' ' + _this.data.selectedCity.area_name,
      isShow: false,
      city_id: _this.data.selectedCity.area_id,
      area_id: _this.data.selectedDistrict.area_id,

      selectedarea_id: _this.data.showDistrict ? _this.data.selectedDistrict.area_id : _this.data.selectedCity.area_id
    })

    console.log(_this.data);
  },
  /**
   * 取消选择
   */
  cancel: function (_this) {
    _this.setData({
      isShow: false
    })
  },
  /**
   * 页面选址触发事件
   */
  choosearea: function (_this) {
    _this.setData({
      isShow: true
    })
  }
}

module.exports = {
  SA: selectArea
}